package quartzDemo;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import lombok.RequiredArgsConstructor;
import org.quartz.*;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;

//@Component
@RequiredArgsConstructor
public class QuartzDemo implements ApplicationRunner {

    private final Scheduler scheduler;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        //jobDetail
        JobDetail jobDetail = JobBuilder
                .newJob(TestJob.class)
                .withIdentity("jobName", "jobGroup")
                .usingJobData("runSecond", 5)//开启后停顿5秒钟
                .build();
        //触发器
       /* SimpleTrigger trigger = TriggerBuilder
                .newTrigger()
                .withIdentity("triggerName", "triggerGroup")
                .startAt(DateUtil.date(1645459285000L))
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(5).repeatForever())
                .endAt(DateUtil.date(1645462885000L))
                .build();*/
        DailyTimeIntervalScheduleBuilder scheduleBuilder = DailyTimeIntervalScheduleBuilder.dailyTimeIntervalSchedule()
                .startingDailyAt(TimeOfDay.hourAndMinuteAndSecondFromDate(DateUtil.parseDateTime("2022-02-22 00:00:02")))//每天的开始时间
                .endingDailyAt(TimeOfDay.hourAndMinuteAndSecondFromDate(DateUtil.parseDateTime("2022-02-23 23:59:59")))//每天的结束时间
                .withIntervalInSeconds(5 + 10)//开启后停顿5秒，关闭后停顿10秒
                .withMisfireHandlingInstructionDoNothing();//禁止quartz在启动后会立即执行一次
        //若选定有星期，需再设置星期，1代表星期日
        scheduleBuilder.onDaysOfTheWeek(CollUtil.newHashSet(1, 2, 3, 4, 5, 6, 7));
        //触发器
        DailyTimeIntervalTrigger trigger = TriggerBuilder.newTrigger().withIdentity("triggerName", "triggerGroup")
                .startAt(DateUtil.parseDateTime("2022-02-20 02:02:02"))//开始日期
                .endAt(DateUtil.endOfDay(DateUtil.parseDateTime("2022-02-28 02:02:02")))//关闭日期
                .withSchedule(scheduleBuilder)
                .build();
        scheduler.scheduleJob(jobDetail, trigger);
        scheduler.start();
    }

}
